package cq.zhcqk.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import cq.zhcqk.R;
import cq.zhcqk.activity.ScanDetailActivity;

/**
 * Created by zhangle on 2018/1/23.
 */
public class MyExpandableListViewAdapter extends BaseExpandableListAdapter {
    private Context context;
    private List<Map<String,Object>> list;

    public MyExpandableListViewAdapter(Context context, ArrayList<Map<String,Object>> list1)
    {
        this.context = context;
        this.list = list1;
    }

    /**
     *
     * 获取组的个数
     *
     * @return
     * @see android.widget.ExpandableListAdapter#getGroupCount()
     */
    @Override
    public int getGroupCount()
    {
//        return group_list.size();
        return list.size();
    }

    /**
     *
     * 获取指定组中的子元素个数
     *
     * @param groupPosition
     * @return
     * @see android.widget.ExpandableListAdapter#getChildrenCount(int)
     */
    @Override
    public int getChildrenCount(int groupPosition)
    {
//        return item_list.get(groupPosition).size();
        Object obj = list.get(groupPosition).get("children");
        if(obj != null){
            return ((List<Map<String, Object>>)(list.get(groupPosition).get("children"))).size();
        }
        return 0;
    }

    /**
     *
     * 获取指定组中的数据
     *
     * @param groupPosition
     * @return
     * @see android.widget.ExpandableListAdapter#getGroup(int)
     */
    @Override
    public Object getGroup(int groupPosition)
    {
//        return group_list.get(groupPosition);
        return list.get(groupPosition);
    }

    /**
     *
     * 获取指定组中的指定子元素数据。
     *
     * @param groupPosition
     * @param childPosition
     * @return
     * @see android.widget.ExpandableListAdapter#getChild(int, int)
     */
    @Override
    public Object getChild(int groupPosition, int childPosition)
    {
//        return item_list.get(groupPosition).get(childPosition);
        return ((List<Map<String, Object>>)(list.get(groupPosition).get("children"))).get(childPosition);
    }

    /**
     *
     * 获取指定组的ID，这个组ID必须是唯一的
     *
     * @param groupPosition
     * @return
     * @see android.widget.ExpandableListAdapter#getGroupId(int)
     */
    @Override
    public long getGroupId(int groupPosition)
    {
        return groupPosition;
    }

    /**
     *
     * 获取指定组中的指定子元素ID
     *
     * @param groupPosition
     * @param childPosition
     * @return
     * @see android.widget.ExpandableListAdapter#getChildId(int, int)
     */
    @Override
    public long getChildId(int groupPosition, int childPosition)
    {
        return childPosition;
    }

    /**
     *
     * 组和子元素是否持有稳定的ID,也就是底层数据的改变不会影响到它们。
     *
     * @return
     * @see android.widget.ExpandableListAdapter#hasStableIds()
     */
    @Override
    public boolean hasStableIds()
    {
        return true;
    }

    /**
     *
     * 获取显示指定组的视图对象
     *
     * @param groupPosition 组位置
     * @param isExpanded 该组是展开状态还是伸缩状态
     * @param convertView 重用已有的视图对象
     * @param parent 返回的视图对象始终依附于的视图组
     * @return
     * @see android.widget.ExpandableListAdapter#getGroupView(int, boolean, android.view.View,
     *      android.view.ViewGroup)
     */
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent)
    {
        GroupHolder groupHolder = null;
        if (convertView == null)
        {
            convertView = LayoutInflater.from(context).inflate(R.layout.gz_list_item, null);
            groupHolder = new GroupHolder();
            groupHolder.txt = (TextView)convertView.findViewById(R.id.gzText);
            groupHolder.img = (ImageView)convertView.findViewById(R.id.zk);
            convertView.setTag(groupHolder);
        }
        else
        {
            groupHolder = (GroupHolder)convertView.getTag();
        }

        if (!isExpanded)
        {
            groupHolder.img.setImageResource(R.mipmap.sq);
        }
        else
        {
            groupHolder.img.setImageResource(R.mipmap.zk);
        }

        groupHolder.txt.setText(list.get(groupPosition).get("name")+"");
        return convertView;
    }

    /**
     *
     * 获取一个视图对象，显示指定组中的指定子元素数据。
     *
     * @param groupPosition 组位置
     * @param childPosition 子元素位置
     * @param isLastChild 子元素是否处于组中的最后一个
     * @param convertView 重用已有的视图(View)对象
     * @param parent 返回的视图(View)对象始终依附于的视图组
     * @return
     * @see android.widget.ExpandableListAdapter#getChildView(int, int, boolean, android.view.View,
     *      android.view.ViewGroup)
     */
    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent)
    {
        ItemHolder itemHolder = null;
        if (convertView == null)
        {
            convertView = LayoutInflater.from(context).inflate(R.layout.gz_list_item_detail, null);
            itemHolder = new ItemHolder();
            itemHolder.txt1 = (TextView)convertView.findViewById(R.id.text1);
            itemHolder.txt2 = (TextView)convertView.findViewById(R.id.text2);
            itemHolder.txt3 = (TextView)convertView.findViewById(R.id.text3);
//            itemHolder.img = (ImageView)convertView.findViewById(R.id.img);
            convertView.setTag(itemHolder);
        }
        else
        {
            itemHolder = (ItemHolder)convertView.getTag();
        }
        final Map<String, Object> childMap = ((List<Map<String, Object>>)(list.get(groupPosition).get("children"))).get(childPosition);
        itemHolder.txt1.setText(childMap.get("name")+"");
        itemHolder.txt2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String du = childMap.get("detailUrl")+"";
                if(!"".equals(du) && du!=null && !"null".equals(du)){
                    Intent intent = new Intent(context,ScanDetailActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putString("type", "detailUrl");
                    bundle.putString("val", du);
                    bundle.putString("title", childMap.get("name")+"");
                    intent.putExtras(bundle);
                    context.startActivity(intent);
                }
            }
        });
        itemHolder.txt3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                Intent intent = new Intent(context,JdMapActivity.class);
                String address = "address="+ childMap.get("address");
//                intent.putExtra("address",childMap.get("address")+"");
//                context.startActivity(intent);
                String uristr = "http://api.map.baidu.com/geocoder?" + address
                        + "&output=html&src=yhc";
                Uri uri = Uri.parse(uristr);
                Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                context.startActivity(intent);
            }
        });
//        itemHolder.img.setBackgroundResource(item_list2.get(groupPosition).get(childPosition));
        return convertView;
    }

    /**
     *
     * 是否选中指定位置上的子元素。
     *
     * @param groupPosition
     * @param childPosition
     * @return
     * @see android.widget.ExpandableListAdapter#isChildSelectable(int, int)
     */
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition)
    {
        return true;
    }

}

class GroupHolder
{
    public TextView txt;

    public ImageView img;
}

class ItemHolder
{
//    public ImageView img;

    public TextView txt1;
    public TextView txt2;
    public TextView txt3;
}
